/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-14 19:47:12
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-20 22:08:07
 * @FilePath: \郭招娣\hou\app\controller\articleLists.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-12 10:40:26
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-16 22:12:08
 * @FilePath: \郭招娣\hou\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const Controller = require('egg').Controller;
const OSS = require('ali-oss');

let client = new OSS({
    // region以杭州为例（oss-cn-hangzhou），其他region按实际情况填写。
    region: 'oss-cn-hangzhou',
    // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
    accessKeyId: 'LTAI5tDJjRUfKVCiv36NZLkt',
    accessKeySecret: 'Czeyav1k6IBbQQWu4FTJBV3sobiYJV',
    // 填写Bucket名称。
    bucket: 'tssheng123',
});
class knowledgeLists extends Controller {
    // 获取文章列表

    async knowledgeLists() {
        const { ctx } = this;
        const { user, status, pageCode, pageSize } = ctx.request.body;
        console.log(user, '11')
        let lists = await this.app.mysql.select('knowledgeLists');
        console.log(lists.length)
        
        let newLists = lists;
        
        if (user) {
            newLists = newLists.filter(v => v.title.includes(user))
        }
        if (status) {
            newLists = newLists.filter(v => v.state.includes(status))
        }
        let total = newLists.length;
        newLists = newLists.slice((pageCode - 1) * pageSize, pageCode * pageSize)
        if (newLists.length) {
            ctx.body = {
                code: 200,
                total,
                results: newLists
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '获取数据失败'
            }
        }
    }

    // 删除
    async deleteKnowledgeLists() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('knowledgeLists', { id });
        if (result) {
            const list = await this.app.mysql.select('knowledgeLists');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list
            }
        } else {
            ctx.body = {
                code: 200,
                msg: '删除失败',
                list
            }
        }

    }

    // 添加标签
    async AddKnowledgeLists() {
        let { ctx } = this;
        let { title, name,img } = ctx.request.body;
        // console.log(title, classValue)
        const post = await this.app.mysql.get('knowledgeLists', { title });
        if (post) {
            ctx.body = {
                msg: '已存在',
                code: 404
            }
        } else {
            const list = await this.app.mysql.insert('knowledgeLists', { title, name ,state:'false'});

            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '添加成功',
                    list: list
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '添加失败',
                }
            }
        }

    }
    // 发布线上 设为草稿
    async knowledgePublish(){

        const { ctx }=this;
        const { state ,id } =ctx.request.body;
        console.log(state,id)
        if (state =='true'){
            const row = {
                state:'false'
            };

            const options = {
                where: {
                    id
                }
            };
            const result = await this.app.mysql.update('knowledgeLists', row, options);
            if(result){
                ctx.body={
                    code:200,
                    result,
                    msg:'发布线上',
                }
            }else{
                ctx.body = {
                    code: 500,
                    result,
                    msg: '设为草稿成功',
                }
            }
        } else if (state == 'false'){
            const row = {
                state: 'true'
            };

            const options = {
                where: {
                    id
                }
            };
            const result = await this.app.mysql.update('knowledgeLists', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '设为草稿成功',
                }
            } else {
                ctx.body = {
                    code: 500,
                    msg: '发布线上',
                }
            }
        }
    }


    // 上传
    async knowUpload() {
        let { ctx } = this;
        let file = ctx.request.files[0];
        console.log(file);
        try {
            const result = await client.put(file.filename, file.filepath);
            ctx.body = {
                code: 200,
                msg: '上传成功',
                result
            }
        } catch (err) {
            ctx.body = {
                code: 400,
                msg: '上传失败',
                err
            }
        }
    }

    async knowList() {
        const { ctx } = this;
        try {
            let result = await client.list();
            console.log(result,'kk')
            // console.log(result)
            ctx.body = {
                code: 200,
                msg: '获取成功',
                urlList: result.objects
            }
        } catch (err) {
            ctx.body = {
                code: 400,
                msg: '获取失败',
                err
            }
        }
    }
}

module.exports = knowledgeLists;
